package 链表与双指针.m02d28.t02;

import java.util.LinkedList;
import java.util.Stack;

/**
 * Definition for singly-linked list.

 */
class ListNode {
    int val;
    ListNode next;
    ListNode() {}
    ListNode(int val) { this.val = val; }
    ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
public class Solution {
    public static void main(String[] args) {
        ListNode node1 = new ListNode(1);
        ListNode node2 = new ListNode(2);
        ListNode node3 = new ListNode(2);
        ListNode node4 = new ListNode(1);
        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = null;
        isPalindrome(node1);
    }
    public static boolean isPalindrome(ListNode head) {
        if(head == null || head.next == null) {
            return true;
        }
        ListNode node = new ListNode();
        ListNode slow = head, fast = head;
        while(fast != null && fast.next != null) {
            node = slow;
            // System.out.println("slow.val: "+slow.val);
            slow = slow.next;
            fast = fast.next.next;
        }


        if(fast != null) {
            slow = slow.next;
        }
        while(slow!=null) {
            if(node.val != slow.val) {
                return false;
            }
            node = node.next;
            slow = slow.next;
        }
        return true;
    }
}